package com.teapos.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teapos.service.TradeService;
import com.teapos.service.UserService;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

/**
 * 登陆controller
 * @author kangrui
 *
 */
@SuppressWarnings("rawtypes")
@RequestMapping("/login")
@Controller
public class LoginController extends BaseController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private TradeService tradeService;

	/**
	 * 登陆校验
	 * @param session
	 * @param name
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/check")
	public String check(HttpServletRequest req,  String name, String password) {
		System.err.println(Maps.newHashMap("username", name, "password", password));
		Map user = userService.checkLogin(Maps.newHashMap("username", name, "password", password));
		if(user!=null) {
			req.getSession().setAttribute(LOGIN_USER, Maps.put(user, "stores", tradeService.getUserStores(Tool.toInt(user.get("id")))));
			return success(null);
		}
		return fail(null);
	}
	
	/**
	 * 跳转首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute(LOGIN_USER);
		return "redirect:/";
	}
	
}
